<?php


class CompetencyController extends RestController
{
	/**
	 * The get action handles GET requests and receives an 'id' parameter; it
	 * should respond with the server resource state of the resource identified
	 * by the 'id' value.
	 */
	public function getAction()
	{
		$compid = $this->_getParam('id');
		$q = new  CompetencyQuery();
		$result = $q->findPK($compid);
		if ($result == null)
		{
			$this->getResponse()->setHttpResponseCode(400);
		}
		else
		{
			$this->getResponse()->append(Zend_Json::encode($result->toArray()));
		}		
	}
	/**
	 * The post action handles POST requests; it should accept and digest a
	 * POSTed resource representation and persist the resource state.
	 */
	public function postAction()
	{		
		if($this->getRequest()->isPost()){
			$result = new  Competency();
			$compname = $this->_getParam('name');
			$createtime = $this->_getParam('createtime');
			$description = $this->_getParam('description');
			$result->setName($compname);
			$result->setDescription($description);
			$result->setCreatetime($createtime);
			if ($result->validate())
			{
				$result->save();
			}
			else
			{
				$this->getResponse()->setHttpResponseCode(500);  
				  // Something went wrong.
				  // Use the validationFailures to check what
				  foreach ($result->getValidationFailures() as $failure) {
				    echo $failure->getMessage();
				  }
			}
		}
		else
		{
			errorResponse();
		}
	}
	private function errorResponse()
	{
		$this->getResponse()->setHttpResponseCode(500);
		$this->getResponse()->appendBody('Your Update Competency data is wrong!');		
	}
	/**
	 * The put action handles PUT requests and receives an 'id' parameter; it
	 * should update the server resource state of the resource identified by
	 * the 'id' value.
	 */
	public function putAction()
	{		
		$compid = $this->_getParam('id');
		if (!empty($compid) && $this->getRequest()->isPut())
		{
			$q = new  CompetencyQuery();
			$result = $q->findPK($compid);
			$compid = $this->_getParam('compid');
			$compname = $this->_getParam('name');
			$createtime = $this->_getParam('createtime');
			$description = $this->_getParam('description');
			$result->setCompid($compid);
			$result->setName($compname);
			$result->setDescription($description);
			$result->setCreatetime($createtime);
			
			if($result->validate())
			{
				$result->save();
			}
			else						
			{
			  $this->getResponse()->setHttpResponseCode(500);  
			  // Something went wrong.
			  // Use the validationFailures to check what
			  foreach ($objUser->getValidationFailures() as $failure) {
			    echo $failure->getMessage();
			  }
			}
		}
		else		
		{
			$this->getResponse()->setHttpResponseCode(500);
		}
	}

	/**
	 * The delete action handles DELETE requests and receives an 'id'
	 * parameter; it should update the server resource state of the resource
	 * identified by the 'id' value.
	 */
	public function deleteAction()
	{
		$compid = $this->_getParam('id');
		if (!empty($compid) && $this->getRequest()->isDelete())
		{
			$result= CompetencyQuery::create()->findOneByCompid(int($compid));
			$result->delete();
		}
		else
		{
			errorResponse();
		}
	}
}

